<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>项目管理</el-breadcrumb-item>
      <el-breadcrumb-item>礼包方法查询</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <!-- 发放时间 -->
        <el-form-item style="width: 400px">
          <el-col :span="7">
            <el-select
              v-model="queryInfo.query.time_type"
              placeholder="请选择"
              size="mini"
            >
              <el-option label="发放时间" value="1"></el-option>
            </el-select>
          </el-col>
          <el-col :span="8">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.start_time"
              size="mini"
              style="width: 100%"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
          <el-col class="line" :span="1" style="text-align: center;">-</el-col>
          <el-col :span="8">
            <el-date-picker
              type="date"
              placeholder="选择日期"
              v-model="queryInfo.query.end_time"
              size="mini"
              style="width: 100%"
              value-format="yyyy-MM-dd"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="项目名称" prop="project_id">
          <el-select
            v-model="queryInfo.query.project_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.get_project"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="礼包名称" prop="gift_id">
          <el-select
            v-model="queryInfo.query.gift_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.gift"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发放人" prop="send_user">
          <el-select
            v-model="queryInfo.query.send_user"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.user"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="关键字" prop="key">
          <el-input
            v-model="queryInfo.query.key"
            placeholder="请输入手机号、流水号或批次号"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="list" border stripe>
        <el-table-column label="批次" prop="bath" align="center"></el-table-column>
        <el-table-column label="发放流水号" prop="main_serial" align="center" width="150px"></el-table-column>
        <el-table-column label="发放时间" prop="create_time" align="center" width="150px"></el-table-column>
        <el-table-column label="手机号码" min-width="150px" max-width="300px" prop="mobile" align="center"></el-table-column>
        <el-table-column label="礼包名称【ID】" min-width="120px" align="center">
          <template slot-scope="scope">
            {{ scope.row.gift_name }}【{{ scope.row.gift_id }}】
          </template>
        </el-table-column>
        <el-table-column label="发放数量" align="center" prop="num"></el-table-column>
        <el-table-column label="归属机构" min-width="150px" max-width="300px" align="center" prop="org_name"></el-table-column>
        <el-table-column label="归属项目" prop="status.name" min-width="200px" align="center">
          <template slot-scope="scope">
            {{ scope.row.project_name }}【{{ scope.row.project_id }}】
          </template>
        </el-table-column>
        <el-table-column label="发放人" min-width="120px" align="center" prop="creator_name"></el-table-column>
        <el-table-column
          label="备注"
          prop="stander"
          min-width="150px"
          max-width="300px"
        >

        </el-table-column>

      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          time_type: '1',
          start_time: '',
          end_time: '',
          project_id: '',
          gift_id: '',
          send_user: '',
          key: ''
        },
        pagenum: 1,
        pagesize: 20,
        key: ''
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false
    }
  },
  created() {
    this.getSelectData()
    this.initDate()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'get_project,gift,user'
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    // 初始化日期
    initDate() {
      const endDate = this.getFormatDate(new Date()).substr(0, 11)
      const beginDate = this.getFormatDate(new Date(new Date() - 3600 * 1000 * 24 * 29)).substr(0, 11)
      this.queryInfo.query.start_time = beginDate
      this.queryInfo.query.end_time = endDate
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/gift/giftRecord',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/gift/downloadRecord',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '礼包发放记录.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          this.$message.error(err)
        })
    }
  }
}
</script>

<style lang="less" scoped>
.line {
  text-align: center;
}
.greyTitle {
  color: #999;
}
</style>
